<?php

namespace app\shop\controller;

use think\Controller;

class Address extends Controller
{
  // 继承底层构造函数
  public function __construct()
  {
    parent::__construct();

    $this->BusinessModel = model('Business.Business');
    $this->AddressModel = model('Business.Address');


    // 接收该用户
    $busid = $this->request->param('busid', '0', 'trim');

    // 查询
    $this->business = $this->BusinessModel->where(['id' => $busid])->find();

    // 判断
    if (!$this->business) {
      $this->error('用户不存在');
      exit;
    }
  }

  // 收货地址列表
  public function index()
  {
    // 判断是否有post请求
    if ($this->request->isPost()) {
      // 执行查询语句
      $result = $this->AddressModel->where('busid', $this->business['id'])->select();

      // 判断
      if ($result) {
        $this->success('查询收获地址成功', null, $result);
        exit;
      } else {
        $this->error('暂无地址');
        exit;
      }
    }
  }

  // 添加收货地址
  public function add()
  {
    // 判断是否有post请求
    if ($this->request->isPost()) {
      // 接收参数
      $params = $this->request->param();

      // 接收code值和status状态
      $code = $this->request->param('code', '', 'trim');
      $status = $this->request->param('status', '0', 'trim');

      // 将code值转换为省市区三个值
      if (!empty($code)) {
        // 链表查询出region里面的parentpath
        $parent = model('Region')->where(['code' => $code])->value('parentpath');

        // 判断
        if (!empty($parent)) {
          $arr = explode(',', $parent);
          $params['province'] = isset($arr[0]) ? $arr[0] : null;
          $params['city'] = isset($arr[1]) ? $arr[1] : null;
          $params['district'] = isset($arr[2]) ? $arr[2] : null;
        }
      }

      // 开启事务
      $this->AddressModel->startTrans();

      // 判断是否选择了默认地址，如果选择了就去更新覆盖
      if ($status == '1') {
        // 直接去将原本的status状态更新为0，然后再将现在选择的更更新为1
        $AddressStatus = $this->AddressModel->where(['busid' => $this->business['id']])->update(['status' => '0']);

        if ($AddressStatus === FALSE) {
          $this->error('更新默认地址状态有误');
          exit;
        }
      }
      // 执行插入语句
      $result = $this->AddressModel->validate('common/Business/Address')->save($params);

      // 判断
      if ($result === FALSE) {
        $this->AddressModel->rollback();
        $this->error($this->AddressModel->getError());
        exit;
      } else {
        $this->AddressModel->commit();
        $this->success('添加成功');
        exit;
      }
    }
  }

  // 查询收货地址
  public function info()
  {
    // 接收参数
    $id = $this->request->param('id', 0, 'trim');

    // 条件
    $where = [
      'id' => $id,
      'busid' => $this->business['id']
    ];

    // 执行查询语句
    $result = $this->AddressModel->where($where)->find();

    // 判断，并将数据返回
    if ($result) {
      $this->success('返回收货地址', null, $result);
      exit;
    } else {
      $this->error('地址不存在');
      exit;
    }
  }

  // 编辑收货地址
  public function edit()
  {
    // 判断是否有post请求
    if ($this->request->isPost()) {
      // 接收参数
      $params = $this->request->param();
      // 接收参数
      $id = $this->request->param('id', 0, 'trim');
      $where = [
        'id' => $id,
        'busid' => $this->business['id']
      ];

      $address = $this->AddressModel->where($where)->find();

      if (!$address) {
        $this->error('收货地址不存在');
        exit;
      }

      // 接收code值和status状态
      $code = $this->request->param('code', '', 'trim');
      $status = $this->request->param('status', '0', 'trim');

      // 将code值转换为省市区三个值
      if (!empty($code)) {
        // 链表查询出region里面的parentpath
        $parent = model('Region')->where(['code' => $code])->value('parentpath');

        // 判断
        if (!empty($parent)) {
          $arr = explode(',', $parent);
          $params['province'] = isset($arr[0]) ? $arr[0] : null;
          $params['city'] = isset($arr[1]) ? $arr[1] : null;
          $params['district'] = isset($arr[2]) ? $arr[2] : null;
        }
      }

      // 开启事务
      $this->AddressModel->startTrans();

      // 判断是否选择了默认地址，如果选择了就去更新覆盖
      if ($status == '1') {
        // 直接去将原本的status状态更新为0，然后再将现在选择的更更新为1
        $AddressStatus = $this->AddressModel->where(['busid' => $this->business['id']])->update(['status' => '0']);

        if ($AddressStatus === FALSE) {
          $this->error('更新默认地址状态有误');
          exit;
        }
      }
      // 执行更新语句，编辑
      $result = $this->AddressModel->validate('common/Business/Address')->isUpdate(true)->save($params);

      // 判断
      if ($result === FALSE) {
        $this->AddressModel->rollback();
        $this->error($this->AddressModel->getError());
        exit;
      } else {
        $this->AddressModel->commit();
        $this->success('编辑成功');
        exit;
      }
    }
  }

  // 删除收货地址
  public function del()
  {
    // 判断是否有post请求
    if ($this->request->isPost()) {
      // 获取参数
      $id = $this->request->param('id', 0, 'trim');

      $where = [
        'id' => $id,
        'busid' => $this->business['id']
      ];

      $address = $this->AddressModel->where($where)->find();

      if (!$address) {
        $this->error('收货地址不存在');
        exit;
      }

      // 进行软删除语句
      $result = $this->AddressModel->destroy($id);

      if ($result === FALSE) {
        $this->error('删除失败');
        exit;
      } else {
        $this->success('删除成功');
        exit;
      }
    }
  }
}
